#-----------------------------------------------------------------------------
# Emergency powers of international organizations
# Between normalization and containment
# Oxford University Press (2019)
# Dr. Christian Kreuder-Sonnen (WZB)
# Uploaded: January 24, 2019
#-----------------------------------------------------------------------------


## Set working directory

setwd()



## Load packages--------------------------------------------------------------

library(stringr)
library(zoo)
library(ggplot2)
library(scales)



## Create crisis perception dictionary----------------------------------------

# Import data for dictionary 

ebola <- read.csv(# set path to dictionary Humanitarian / Health Crisis, 
  header = FALSE, strip.white = TRUE)
euro <- read.csv(# set path to dictionary Economic and Financial Crisis, 
  header = FALSE, strip.white = TRUE)
fuku <- read.csv(# set path to dictionary Energy and Environment Crisis, 
  header = FALSE, strip.white = TRUE)
terror <- read.csv(# set path to dictionary Security Crisis,
  header = FALSE, strip.white = TRUE)

# Clean dictionary data

dict <- rbind(ebola, euro, fuku, terror) # create consolidated dictionary 
dict <- as.data.frame(tolower(dict$V1)) # everything to lower case
dict <- unique(dict) # delete duplicates
row.names(dict) <- NULL # delete row.names columm
names(dict)[1] <- paste("term") # Renaming variable
dict <- as.data.frame(sort(dict$term)) # sort alphabetically
names(dict)[1] <- paste("term") # Renaming variable
dict <- paste(dict$term, collapse ="| ") # collapse into one string with OR condition

rm(ebola, euro, fuku, terror) # remove objects



## Creating the corpus function--------------------------------------------------

LexClean <- function (path) { # function that expects the path with the TXT files retrieved from Lexis Nexis
  
  # Importing the text
  #-------------------
  
  setwd(path)
  
  # Reading all txt-files in within PATH, individual lines as vectors
  # Collapsing to a single string, then combine to one corpus
  
  filenames <- list.files(pattern = ".txt", ignore.case = TRUE, recursive = TRUE) # List of all txt files in PATH (including subfolders)
  news_raw <- data.frame() # Empty data frame, the target
  
  for(i in filenames) {
    news_temp <- readLines(i)
    news_temp <- gsub("BYLINE:.*", " ", news_temp) # Here certain document markers are removed, exploiting the fact that they are saved as individual lines
    news_temp <- gsub("DATELINE:.*", " ", news_temp)
    news_temp <- gsub("LOAD-DATE:.*", " ", news_temp)
    news_temp <- gsub("LANGUAGE: ENGLISH", " ", news_temp)
    news_temp <- gsub("Copyright [0-9]{4}.*", " ", news_temp)
    news_temp <- paste(news_temp, collapse=" ") # Collapsing individual lines to a common string
    news_raw <- paste(news_raw, news_temp) # Paste string from file i to the news-raw dataframe
    rm(news_temp)
  }
  
  rm(filenames, i)
  
  
  # Create article-based data frame
  #--------------------------------
  
  # Splitting into individual articles (based on LexNex "Dokument" markup pattern)
  rawdocs <- strsplit(news_raw, split = "Dokument [0-9]+ von [0-9]+", fixed = FALSE)
  
  rm(news_raw)
  
  # Turn this into a data frame with one oberservation per article
  articles <- as.data.frame(rawdocs, row.names = NULL)
  articles <- as.data.frame(articles[-1, ]) # Removing the faulty first line 
  names(articles)[1] <- paste("raw_text") # Renaming variable
  
  # Extracting some descriptive variables from raw article texts
  #-------------------------------------------------------------
  
  # Article length
  
  lengthhelp <- as.data.frame(str_extract(articles$raw_text, "LENGTH: [0-9]+ words|LENGTH: [0-9]+  words|LENGTH:  [0-9]+ words")) # Extracting the article word count as deliverred by LexNex
  articles$raw_text <- str_replace(articles$raw_text, "LENGTH: [0-9]+ words|LENGTH: [0-9]+  words|LENGTH:  [0-9]+ words", " ") # Removing the parsed information from the raw text
  
  names(lengthhelp)[1] <- paste("raw_length") # Renaming variable
  
  length <- as.data.frame(str_extract(lengthhelp$raw_length, "[0-9]+"))
  names(length)[1] <- paste("article_length") # Renaming variable
  
  rm(lengthhelp)
  
  # Publication date
  
  pubdatehelp <- as.data.frame(str_extract(articles$raw_text, "(January|February|March|April|May|June|July|August|September|October|November|December) [0-9]+, [0-9]{4}"))
  articles$raw_text <- str_replace(articles$raw_text, "(January|February|March|April|May|June|July|August|September|October|November|December) [0-9]+, [0-9]{4}, (Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)", " ")
  articles$raw_text <- str_replace(articles$raw_text, "(January|February|March|April|May|June|July|August|September|October|November|December) [0-9]+, [0-9]{4} (Monday|Tuesday|Wednesday|Thursday|Friday|Saturday|Sunday)", " ") # Sometimes, the weekday was not separated by a comma
  articles$raw_text <- str_replace(articles$raw_text, "(January|February|March|April|May|June|July|August|September|October|November|December) [0-9]+, [0-9]{4}", " ") # Sometimes, the weekday is not given at all
  
  names(pubdatehelp)[1] <- paste("pub_date") # Renaming variable
  pubdatehelp <- as.data.frame(str_replace(pubdatehelp$pub_date, ",", ""))
  names(pubdatehelp)[1] <- paste("pub_date") # Renaming variable
  
  Sys.setlocale("LC_TIME", "English")
  pubdate <- as.data.frame(as.Date(pubdatehelp$pub_date, "%B %d %Y"))
  names(pubdate)[1] <- paste("pub_date") # Renaming variable
  
  rm(pubdatehelp)
  
  
  # Page number
  
  pagehelp <- as.data.frame(str_extract(articles$raw_text, "SECTION:.*(Pg\\.|Page) ([0-9]+|I|II|III|IV|V|VI|VII|VIII|IX|X)")) # Sometimes page numbers are given in roman numbers
  articles$raw_text <- str_replace(articles$raw_text, "SECTION:.*?(Pg\\.|Page) ([0-9]+|I|II|III|IV|V|VI|VII|VIII|IX|X)", " ") # some articles have a 'page' in the end
  
  names(pagehelp)[1] <- paste("page") # Renaming variable
  
  pagehelp$page <- str_replace(pagehelp$page, "SECTION:.*(Pg\\.|Page)", "") # Removes everything until the page number comes 
  page <- str_extract(pagehelp$page, "[0-9]+|I|II|III|IV|V|VI|VII|VIII|IX|X") # Extracts the first arabic or roman number that appears
  
  
  rm(pagehelp)
  
  page <- str_replace(page, "VIII", "8")
  page <- str_replace(page, "VII", "7")
  page <- str_replace(page, "VI", "6")
  page <- str_replace(page, "IV", "4")
  page <- str_replace(page, "IX", "9")
  page <- str_replace(page, "III", "3")
  page <- str_replace(page, "II", "2")
  page <- str_replace(page, "I", "1")
  page <- str_replace(page, "V", "5")
  page <- str_replace(page, "X", "10")
  
  page[is.na(page)] <- 0 # Sets all missing page numbers to zero
  
  page  <- as.data.frame(page)
  names(page)[1] <- paste("page") # Renaming variable
  
  # Newspaper source
  
  newspaper <- as.data.frame(str_extract(articles$raw_text, "^[ ]+[A-Za-z]+ [A-Za-z]+")) # First two alpha terms after any number of whitespaces in the beginning of the string
  names(newspaper)[1] <- paste("newspaper") # Renaming variable
  newspaper <- as.data.frame(str_trim(newspaper$newspaper, side = "both")) # Trims the unnecessary white spaces left and right of the source name
  names(newspaper)[1] <- paste("newspaper") # Renaming variable
  
  # Cleaning data frame 
  #--------------------
  
  # Combining the variables
  
  corpus <- as.data.frame(c(newspaper, pubdate, page, length, articles))
  rm(newspaper, pubdate, page, length, articles)
  
  # Removing double entries from LexNex
  
  corpus <- unique(corpus)
  
  # Relabelling newspaper sources 
  
  corpus$newspaper <- str_replace(corpus$newspaper, "The Guardian", "TG")
  corpus$newspaper <- str_replace(corpus$newspaper, "The International", "NYT")
  corpus$newspaper <- str_replace(corpus$newspaper, "The Straits", "ST")
  
  
  
  # Cleaning the raw texts
  #-----------------------
  
  
  # Removing the source markers from raw text
  
  corpus$raw_text <- str_replace_all(corpus$raw_text, "The Guardian - Final Edition|The Guardian \\(London\\)|The Guardian \\(London\\) - Final Edition|- Final Edition|The Guardian|The New York Times|The Straits Times \\(Singapore\\)", "")
  corpus$raw_text <- str_replace_all(corpus$raw_text, "Financial Times \\(London, England\\)", "") # From FT
  corpus$raw_text <- str_replace_all(corpus$raw_text, "Financial Times \\(London\\) \\(London,England\\)", "") # From FT
  corpus$raw_text <- str_replace_all(corpus$raw_text, "The International Herald Tribune", "")
  corpus$raw_text <- str_replace_all(corpus$raw_text, "International New York Times", "")
  corpus$raw_text <- str_replace_all(corpus$raw_text, "Guardian.com.|Guardian.com", "")
  corpus$raw_text <- str_replace_all(corpus$raw_text, "The Straits Times", "")
  corpus$raw_text <- str_replace_all(corpus$raw_text, "The Straits Times", "")
  
  corpus$raw_text <- gsub("[0-9]+:[0-9]{2,} [A-Z]{2,} GMT", "", corpus$raw_text) # remove time from Guardian
  
  
  
  # Removing white spaces
  
  corpus$raw_text <- str_trim(corpus$raw_text)
  
  # Some manual cleaning based on observations of regularties in articles
  
  corpus$raw_text <- str_replace(corpus$raw_text, "Late Edition - Final", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "INTERNATIONAL BUSINESS;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "BUSINESS DIGEST  ;", "") # From NYT
  corpus$raw_text <- str_replace_all(corpus$raw_text, "�", "") # From NYT
  corpus$raw_text <- str_replace_all(corpus$raw_text, "Column [0-9]; [A-Za-z] Desk", "") # From NYT
  corpus$raw_text <- str_replace_all(corpus$raw_text, "Column [0-9], [A-Za-z] Desk", "") # From NYT
  corpus$raw_text <- str_replace_all(corpus$raw_text, "Column [0-9]; [A-Za-z ]{2}Desk", "") # From NYT
  corpus$raw_text <- str_replace_all(corpus$raw_text, "Column [0-9], [A-Za-z ]{2}Desk", "") # From NYT
  
  corpus$raw_text <- str_replace(corpus$raw_text, "INTERNATIONAL BRIEFS;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "INTERNATIONAL BUSINESS;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "NEWS SUMMARY  ;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "WORLD BUSINESS BRIEFING: ASIA;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "WORLD BUSINESS BRIEFING: EUROPE;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "WORLD BUSINESS BRIEFING: AMERICAS;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "WORLD BUSINESS BRIEFING: WORLD TRADE;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "^World Business Briefing", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "World News Briefs;", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "on the web", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "INSIDE  ", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "DIARY  ", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "Chronicle  ", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "Correction appended ", "") # From NYT /FT
  corpus$raw_text <- str_replace(corpus$raw_text, "Late City Final Edition", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "Late City Final Edition", "") # From NYT
  corpus$raw_text <- str_replace(corpus$raw_text, "Late City Final Edition", "") # From NYT
  
  corpus$raw_text <- str_replace(corpus$raw_text, "LONDON EDITION", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "London Edition [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "Europe Edition [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "EUROPE EDITION [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "USA EDITION [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "USA Edition [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "Japan Edition [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "SURVEY EDITION [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "JAPAN EDITION [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "Asia Edition [0-9]", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "London Edition [0-9]", "") # From FT
  
  corpus$raw_text <- str_replace(corpus$raw_text, "FT Exporter", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "FT Guide to the Week", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "COMMENT & ANALYSIS:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "COMPANIES & FINANCE:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "INTERNATIONAL ECONOMY:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "LEADER:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "LETTERS TO THE EDITOR:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "LEX COLUMN:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "WORLD NEWS - TRADE:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "WORLD NEWS: ASIA-PACIFIC:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "WORLD NEWS:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "COMMODITIES & AGRICULTURE:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "INTERNATIONAL ECONOMY:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "News: World Trade:", "") # From FT
  
  corpus$raw_text <- str_replace(corpus$raw_text, "FRONT PAGE - FIRST", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "FRONT PAGE -", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "Front page - first section:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "FT GUIDE TO THE WEEK:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "NATIONAL NEWS:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "LATIN AMERICA & CARIBBEAN:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "OBSERVER:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "SHORTS:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "THE WEEK AHEAD", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "TRADE:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "World Business Briefing", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "World Diary       TODAY", "") # From FT
  
  corpus$raw_text <- str_replace(corpus$raw_text, "Surveys WOR1  SURVEY - WORLD TRADE:", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "Surveys [A-Z]{3}[0-9]{1}", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "SURVEYS [A_Z]{1}", "") # From FT
  corpus$raw_text <- str_replace(corpus$raw_text, "Surveys WOR1  SURVEY - WORLD TRADE:", "") # From FT
  
  
  corpus$raw_text <- str_replace(corpus$raw_text, "HIGHLIGHT:", "") # Section marker
  corpus$raw_text <- str_replace(corpus$raw_text, "GRAPHIC:", "") # Section marker
  
  corpus$raw_text <- str_trim(corpus$raw_text, side = "both") # Removes whitespaces left and right
  corpus$raw_text <- str_replace(corpus$raw_text, "^,", "") # Removes leading commas
  corpus$raw_text <- str_replace(corpus$raw_text, "^[0-9]", "") # Removes leading figures
  
  corpus$raw_text <- gsub("/", " ", corpus$raw_text) # Removes any slash from the text and replaces it with a space
  corpus$raw_text <- gsub("([\\])", "", corpus$raw_text) # Should remove any backslash from the text
  corpus$raw_text <- gsub("\t", " ", corpus$raw_text) # Should remove any tabulator in the raw text and replace it with a simple space
  corpus$raw_text <- gsub(";", ",", corpus$raw_text) # Replaces any semicolon with a comma (so as not to interfere with the separators when exporting)
  
  corpus$raw_text <- str_trim(corpus$raw_text, side = "both") # Removes whitespaces left and right
  
  # Extracting the article headline
  
  corpus$headline <- str_replace(corpus$raw_text, "[ ]{2,}.*", "") # Deletes every character after at least two whitspace occurred (i.e. keeps the first sentence in raw text)
  
  # Removing double entries from LexNex
  
  corpus <- unique(corpus)
  
  # Ensuring that the function outputs the corpus data frame
  
  corpus
  
}


### Creation of policy field specific corpora and application of dictionary ###


## Create corpus for finance policy field and count crisis term occurences-------------------

finance <- LexClean(# set path to corpus Finance) 

# Crisis term count

finance$count <- str_count(tolower(finance$raw_text), dict) # counts every occurence in $raw_text

# Frontpage dummy

finance$frontpage <- finance$page == 1
class(finance$frontpage) <- "numeric"

# Count of topic occurence in headline

finance$financehead <- str_count(finance$headline, regex("finance|finances|financed|financial|financially|currency|currencies|fiscal|budget|budgetary|budgets|monetary|banking|markets|debt", ignore_case = TRUE))

# Count of topic occurence in body

finance$financebody <- str_count(finance$raw_text, regex("finance|finances|financed|financial|financially|currency|currencies|fiscal|budget|budgetary|budgets|monetary|banking|markets|debt", ignore_case = TRUE))

# Delete wrongly matched articles manually

finance <- subset(finance, financebody > 0)

# Crisis index

finance$article_length <- as.numeric(as.character(finance$article_length))

finance$index <- (finance$count/(log(finance$article_length)) * (1 + finance$frontpage + log(2*finance$financehead + finance$financebody)))

# Delete rows with missings on index variable

finance <- subset(finance, index!="NA")



## Aggregating data for finance-----------------------------------------------


# Crisis perception sum monthly

finance$monthly <- as.yearmon(finance$pub_date) # extract yearmon
finance$monthly <- as.Date(finance$monthly) # change to Date format

monthly_crisis_finance <- aggregate(finance$index, by=list(finance$monthly), FUN=sum)
names(monthly_crisis_finance)[1] <- paste("date") # Renaming variable


# Crisis perception sum quarter

finance$qtr <- as.yearqtr(finance$pub_date) # extract yearmon
finance$qtr <- as.Date(finance$qtr) # change to Date format

qtr_crisis_finance <- aggregate(finance$index, by=list(finance$qtr), FUN=sum)
names(qtr_crisis_finance)[1] <- paste("date") # Renaming variable



## create corpus for health policy field and count crisis term occurences------

health <- LexClean(# set path to corpus Health) 

# Crisis term count

health$count <- str_count(tolower(health$raw_text), dict) # counts every occurence in $raw_text

# Frontpage dummy

health$frontpage <- health$page == 1

# Count of topic occurence in headline

health$healthhead <- str_count(health$headline, regex("health|healthy|unhealthy|illness|sick|sicken|sickened|sickening|disease|diseases|medical|medicine|vaccine|vaccinate|vaccination|treatment|treatments|epidemic|epidemics|pandemic|pandemics|infection|infections", ignore_case = TRUE))

# Count of topic occurence in body

health$healthbody <- str_count(health$raw_text, regex("health|healthy|unhealthy|illness|sick|sicken|sickened|sickening|disease|diseases|medical|medicine|vaccine|vaccinate|vaccination|treatment|treatments|epidemic|epidemics|pandemic|pandemics|infection|infections", ignore_case = TRUE))

# Delete wrongly matched articles manually

health <- subset(health, healthbody > 0)

# Crisis index

health$article_length <- as.numeric(as.character(health$article_length))
health$index <- (health$count/(log(health$article_length)) * (1 + health$frontpage + log(2*health$healthhead + health$healthbody)))



## Aggregating data for health-----------------------------------------------------

# Crisis perception sum monthly

health$monthly <- as.yearmon(health$pub_date) # extract yearmon
health$monthly <- as.Date(health$monthly) # change to Date format

monthly_crisis_health <- aggregate(health$index, by=list(health$monthly), FUN=sum)
names(monthly_crisis_health)[1] <- paste("date") # Renaming variable


# Crisis perception sum quarter

health$qtr <- as.yearqtr(health$pub_date) # extract yearmon
health$qtr <- as.Date(health$qtr) # change to Date format

qtr_crisis_health <- aggregate(health$index, by=list(health$qtr), FUN=sum)
names(qtr_crisis_health)[1] <- paste("date") # Renaming variable



## Create corpus for terrorism policy field and count crisis term occurences------

terrorism <- LexClean(# set path to corpus Terrorism)  

# Crisis term count

terrorism$count <- str_count(tolower(terrorism$raw_text), dict) # counts every occurence in $raw_text

# Frontpage dummy

terrorism$frontpage <- terrorism$page == 1

# Count of topic occurence in headline

terrorism$terrorhead <- str_count(terrorism$headline, regex("terror|terrorism|terrorist|terrorists|terroristic|counterterrorism|counterterrorist", ignore_case = TRUE))

# Count of topic occurence in body

terrorism$terrorbody <- str_count(terrorism$raw_text, regex("terror|terrorism|terrorist|terrorists|terroristic|counterterrorism|counterterrorist", ignore_case = TRUE))

# Delete wrongly matched articles manually

terrorism <- subset(terrorism, terrorbody > 0)


# Crisis index

terrorism$article_length <- as.numeric(as.character(terrorism$article_length))
terrorism$index <- (terrorism$count/(log(terrorism$article_length)) * (1 + terrorism$frontpage + log(2*terrorism$terrorhead + terrorism$terrorbody)))



## Aggregating data for terrorism policy field------------------------------------------


# Crisis perception sum monthly

terrorism$monthly <- as.yearmon(terrorism$pub_date) # extract yearmon
terrorism$monthly <- as.Date(terrorism$monthly) # change to Date format

monthly_crisis_terrorism <- aggregate(terrorism$index, by=list(terrorism$monthly), FUN=sum)
names(monthly_crisis_terrorism)[1] <- paste("date") # Renaming variable


# Crisis perception sum quarter

terrorism$qtr <- as.yearqtr(terrorism$pub_date) # extract yearmon
terrorism$qtr <- as.Date(terrorism$qtr) # change to Date format

qtr_crisis_terrorism <- aggregate(terrorism$index, by=list(terrorism$qtr), FUN=sum)
names(qtr_crisis_terrorism)[1] <- paste("date") # Renaming variable


## Plotting--------------------------------------------------------------------------------

# Finance
#--------

# Monthly crisis perception 2006-2016

plot1 <- ggplot() +
  geom_line(data = monthly_crisis_finance, aes(x=date, y=x), colour="darkgrey") +
  geom_smooth(data = monthly_crisis_finance, aes(x=date, y=x), colour="black", fill = "lightgrey", method = "loess", size = 1, span = 0.05, se = FALSE) +
  theme_bw()+
  xlab("") + ylab("") + 
  scale_x_date(labels = date_format("%Y"), breaks = date_breaks("year")) +
  theme(plot.title = element_text(size=12, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 14),
        axis.text.x=element_text(angle=90, size=11, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=11, colour = "black")) 

# Monthly crisis perception 2010-2016

plot2 <- ggplot() +
  geom_line(data = monthly_crisis_finance, aes(x=date, y=x), colour="black") +
  geom_smooth(data = monthly_crisis_finance, aes(x=date, y=x), colour="black", fill = "lightgrey", method = "loess", size = 1, span = 0.05) +
  theme_bw()+
  xlab("") + ylab("") + 
  scale_x_date(labels = date_format("%Y"), breaks = date_breaks("year"), limits = c(as.Date("2010-01-01"), as.Date("2015-12-01"))) +
  theme(plot.title = element_text(size=12, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 14),
        axis.text.x=element_text(angle=90, size=11, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=11, colour = "black")) 


# Health
#--------

# Quarterly crisis perception 01/2000 - 06/2007

plot3 <- ggplot() +
  geom_line(data = qtr_crisis_health, aes(x=date, y=x), colour="black", size = 0.7) +
  theme_bw()+
  xlab("") + ylab("") + 
  scale_x_date(labels = date_format("%Y"), breaks = date_breaks("year"), limits = c(as.Date("2000-01-01"), as.Date("2007-06-01"))) +
  theme(plot.title = element_text(size=12, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 14),
        axis.text.x=element_text(angle=90, size=11, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=11, colour = "black")) 

# Quarterly crisis perception 07/2007 - 12/2013

plot4 <- ggplot() +
  geom_line(data = qtr_crisis_health, aes(x=date, y=x), colour="black", size = 0.7) +
  theme_bw()+
  xlab("") + ylab("") + 
  scale_x_date(labels = date_format("%Y"), breaks = date_breaks("year"), limits = c(as.Date("2007-07-01"), as.Date("2013-12-01"))) +
  annotate("segment", x = as.Date("2009-04-01"), xend = as.Date("2010-10-01"), y = 2500, yend = 2500, size = 0.5, colour = "darkgrey") +
  annotate("segment", x = as.Date("2009-04-01"), xend = as.Date("2009-04-01"), y = 2400, yend = 2600, size = 0.5, colour = "darkgrey") +
  annotate("segment", x = as.Date("2010-10-01"), xend = as.Date("2010-10-01"), y = 2400, yend = 2600, size = 0.5, colour = "darkgrey") +
  annotate("text", x = as.Date("2010-01-15"), y = 2750, label= "Official duration \n of PHEIC") + 
  scale_y_continuous(breaks = c(1000, 2000, 3000, 4000), limits = c(750, 4100)) +
  theme(plot.title = element_text(size=12, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 14),
        axis.text.x=element_text(angle=90, size=11, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=11, colour = "black"))


# Terrorism
#-----------

# Quarterly crisis perception 1997-2015

plot5 <- ggplot() +
  geom_line(data = qtr_crisis_terrorism, aes(x=date, y=x), colour="black", size = 0.7) +
  theme_bw()+
  xlab("") + ylab("") + 
  scale_x_date(labels = date_format("%Y"), breaks = date_breaks("year")) +
  theme(plot.title = element_text(size=12, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 14),
        axis.text.x=element_text(angle=90, size=11, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=11, colour = "black")) 

# Quarterly crisis perception 2002-2013

plot6 <- ggplot() +
  geom_line(data = qtr_crisis_terrorism, aes(x=date, y=x), colour="black", size = 0.7) +
  theme_bw()+
  xlab("") + ylab("") + 
  scale_x_date(labels = date_format("%Y"), breaks = date_breaks("year"), limits = c(as.Date("2002-01-01"), as.Date("2012-10-01"))) +
  scale_y_continuous(breaks = c(0, 400, 800, 1200, 1600), limits = c(0, 1700)) +
  theme(plot.title = element_text(size=12, face="bold"),
        panel.grid.major.y=element_line(linetype=2,color="grey"),
        panel.grid.major.x=element_line(linetype=2,color="grey"),
        panel.grid.minor=element_line(color="white"),
        panel.background=element_rect(fill="white"),
        axis.title.y = element_text(size = 14),
        axis.text.x=element_text(angle=90, size=11, vjust = 0.5, colour = "black"),
        axis.text.y=element_text(size=11, colour = "black")) 
 


# End of script------------------------------------------------------------------------------
